
/*
 * @Author       : ljkt
 * @Version      : 1.0
 * @Date         : 2021-10-08 09:29:52
 * @LastEditors: ljkt
 * @LastEditTime: 2025-03-02 21:22:40
 * @Description  :
 */

#include "./Common_Include.h"

#ifdef EN_PUTCHAR
#include "stdio.h"
char putchar(char ch)
{
	Dev_IoPrint_Byte(ch);
	return ch;
}
#endif
#ifdef PRINT_SPEED_LOW
typedef struct
{
	unsigned short front;
	unsigned short end;
	unsigned char buf[1000ul];
} s_dev_ioprint_t;
Ram s_dev_ioprint_t IoPrint_Data;
#endif

void Dev_IoPrint_Init(void)
{
#ifdef PRINT_SPEED_LOW
	memset(&IoPrint_Data, 0, sizeof(s_dev_ioprint_t));
#endif
	Dev_IoPrint_Baud();
}
void Dev_IoPrint_Byte(unsigned char byte)
{

#ifdef PRINT_SPEED_LOW
	IoPrint_Data.end++;
	if (IoPrint_Data.end >= sizeof(IoPrint_Data.buf))
	{
		IoPrint_Data.end = 0;
	}
	IoPrint_Data.buf[IoPrint_Data.end] = byte;
#else
	unsigned char dat = 0x02;
	SET_GLOBAL_IE(REF_FALSE);
	Dev_IoPrint_Tx_Bit(dat & 0x01);
	Dev_IoPrint_Tx_Bit(byte & 0x01);
	Dev_IoPrint_Tx_Bit(byte & 0x02);
	Dev_IoPrint_Tx_Bit(byte & 0x04);
	Dev_IoPrint_Tx_Bit(byte & 0x08);
	Dev_IoPrint_Tx_Bit(byte & 0x10);
	Dev_IoPrint_Tx_Bit(byte & 0x20);
	Dev_IoPrint_Tx_Bit(byte & 0x40);
	Dev_IoPrint_Tx_Bit(byte & 0x80);
	Dev_IoPrint_Tx_Bit(dat & 0x02);
	SET_GLOBAL_IE(REF_TRUE);
#endif
	Hal_Wtd_Feed();
}
void Dev_IoPrint_Scan(void)
{
#ifdef PRINT_SPEED_LOW
	static unsigned char tick = 0;
	static unsigned char step = 0;
	static unsigned short txdata = 0;

	if (Dev_Autoload_Ntick_u8(&tick, 1ul))
	{
		switch (step)
		{
		case 0:
			if (IoPrint_Data.end == IoPrint_Data.front)
			{
				Dev_IoPrint_Tx_Bit(1);
				return;
			}
			IoPrint_Data.front++;
			if (IoPrint_Data.front >= sizeof(IoPrint_Data.buf))
			{
				IoPrint_Data.front = 0;
			}
			txdata = IoPrint_Data.buf[IoPrint_Data.front];
			txdata = (txdata << 1) | 0x0200;
			step = 1;
			break;
		case 1:
			((txdata & 0x0001) ? Dev_IoPrint_Tx_Bit(1) : (Dev_IoPrint_Tx_Bit(0)));
			txdata = txdata >> 1;
			if (txdata == 0)
			{
				step = 0;
			}
			break;
		default:
			step = 0;
			break;
		}
	}
#endif
}

void Dev_IoPrint_Baud(void)
{
	unsigned char i = 0;
	Dev_IoPrint_Byte(0x00);
	while (i < 255)
	{
		i++;
	}
	Dev_IoPrint_Byte(0x55);
	Dev_IoPrint_Byte(0x00);
	Dev_IoPrint_Byte(0x7f);
	Dev_IoPrint_Byte(0x2a);
}

void Dev_IoPrint_Str(CONST_RAM char *str)
{
	while ((*str) != '\0')
	{
		Dev_IoPrint_Byte(*str);
		str++;
	}
}

void Dev_IoPrint_1Byte_HexStr(unsigned char dat)
{
	//  unsigned char Asiic[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	unsigned char Asiic[] = "0123456789ABCDEF";
	Dev_IoPrint_Byte(Asiic[(dat >> 4) & 0x0f]);
	Dev_IoPrint_Byte(Asiic[(dat >> 0) & 0x0f]);
}

void Dev_IoPrint_DecStr(unsigned long dat)
{
	unsigned char i = 0, buf[11];
	unsigned long dat32 = dat;
	memset(buf, 0, sizeof(buf));
	do
	{
		buf[i] = (unsigned char)(dat32 % 10 + '0');
		dat32 /= 10;
		i++;
	} while (dat32);

	while (i)
	{
		i--;
		Dev_IoPrint_Byte(buf[i]);
	}
}

void Dev_IoPrint_Hex(unsigned char *addr, unsigned char len)
{
	unsigned char i = 0;
	while (i < len)
	{
		Dev_IoPrint_Byte(addr[i++]);
	}
}

void Dev_IoPrint_Str_DecStr(CONST_RAM char *str, signed long dat)
{
	Dev_IoPrint_Str(str);
	((dat >= 0) ? (Dev_IoPrint_DecStr(dat)) : (Dev_IoPrint_Byte('-'), Dev_IoPrint_DecStr(dat * (-1))));
	Dev_IoPrint_Str("\n");
}

void Dev_IoPrint_N_Str_DecStr(CONST_RAM char *str, unsigned char offset, signed long *dat, unsigned char cnt)
{
	unsigned char i;
	for (i = 0; i < cnt; i++)
	{
		Dev_IoPrint_Str(&str[i * offset]);
		((dat[i] >= 0) ? (Dev_IoPrint_DecStr(dat[i])) : (Dev_IoPrint_Byte('-'), Dev_IoPrint_DecStr(dat[i] * (-1))));
	}

	Dev_IoPrint_Str("\n");
}

void Dev_IoPrint_Str_Buf2DecStr(CONST_RAM char *str, unsigned char index, signed long dat)
{
	Dev_IoPrint_Str(str);
	Dev_IoPrint_Str("[");
	Dev_IoPrint_DecStr(index);
	Dev_IoPrint_Str("]= ");
	((dat >= 0) ? (Dev_IoPrint_DecStr(dat)) : (Dev_IoPrint_Byte('-'), Dev_IoPrint_DecStr(dat * (-1))));
	Dev_IoPrint_Str("\n");
}

void Dev_IoPrint_Str_Buf2HexStr(CONST_RAM char *str, signed char *dat, unsigned short len)
{
	unsigned short i = 0;
	Dev_IoPrint_Str(str);
	while (i < len)
	{
		Dev_IoPrint_1Byte_HexStr(dat[i]);
		Dev_IoPrint_Byte(' ');
		i++;
	}
	Dev_IoPrint_Str("\n");
}
void Dev_IoPrint_Str_1Byte_Hex(CONST_RAM char *str, unsigned char dat)
{
	Dev_IoPrint_Str(str);
	Dev_IoPrint_1Byte_HexStr(dat);
	Dev_IoPrint_Str("\n");
}
void Dev_IoPrint_4Byte_HexStr(CONST_RAM char *str, unsigned long dat)
{
	Dev_IoPrint_Str(str);
	Dev_IoPrint_1Byte_HexStr(dat >> 24ul);
	Dev_IoPrint_1Byte_HexStr(dat >> 16ul);
	Dev_IoPrint_1Byte_HexStr(dat >> 8ul);
	Dev_IoPrint_1Byte_HexStr(dat >> 0ul);
	Dev_IoPrint_Str("\n");
}